﻿using Karambolo.Extensions.Logging.File;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using BasicLoggerModule.Service;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using log4net.Config;

namespace BasicLoggerModule.Model
{
    public class BasicModel
    {
        public void Run()
        {
            //Trace  Debug Information Warning Error  Critical
            //Trace  Debug 开发环境 调试代码
            //Trace 栈级别 更加详细 一般不使用
            //Debug 基本信息 调试代码

            //Information Warning Error Error Critical  生产环境 生产环境的提示
            //Information  方法正常输出日志 快速知道方法的执行过程
            //Warning 警告日志输出
            //Error 方法出现异常的日志
            //Critical net8提供的组件出现了异常日志 系统死掉了

            #region 日志打印
            {
                ILoggerFactory loggerFactory = LoggerFactory.Create(
                   builder =>
                   {
                       //builder.AddFile(option =>
                       //{
                       //    option.RootPath = AppContext.BaseDirectory;
                       //    option.Files = new[] { new LogFileOptions { Path = "logs/log.txt" } };
                       //});
                       builder.AddConsole().SetMinimumLevel(LogLevel.Information);

                       //自定义日志输出
                       builder.AddSqlserver().SetMinimumLevel(LogLevel.Information);
                   });
                ////Trace  Debug Information Warning Error  Critical
                //ILogger<Program> logger = loggerFactory.CreateLogger<Program>();
                //logger.LogTrace("xxxx");
                //logger.LogDebug("xxxx");
                //logger.LogInformation("xxxx");
                //logger.LogWarning("xxxx");
                //logger.LogError("xxxx");
                //logger.LogCritical("xxxx");
                //logger.LogInfo("wwwwxxxx");
                //Console.ReadKey();
            }
            #endregion

            #region 日志组件的应用
            {
                ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
                configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", true, true);
                IConfiguration configuration = configurationBuilder.Build();
                ServiceCollection services = new ServiceCollection();
                services.AddLogging(options =>
                {
                    options.AddConfiguration(configuration.GetSection("Logging"));
                    options.AddConsole();

                    ////自定义日志输出
                    options.AddSqlserver().SetMinimumLevel(LogLevel.Information);   //自定义MySQL日志输出

                    //options.AddFile(option =>
                    //{
                    //    option.RootPath = AppContext.BaseDirectory;
                    //    option.Files = new[] { new LogFileOptions { Path = "logs/log.txt" } };
                    //});

                    //});
                    //options.AddFile("logs/myapp-{Date}.txt");
                    //options.AddFile(option =>
                    //       {
                    //           var fileConfig = configuration.GetSection("Logging:File");
                    //           option.RootPath = AppContext.BaseDirectory;
                    //           option.Files = new[] { new LogFileOptions { Path = fileConfig["Path"], MaxFileSize = fileConfig.GetValue<long>("MaxSize") } };
                    //       });
                    //options.AddFile();
                });

                services.AddTransient<IProductService, ProductService>();
                var builder = services.BuildServiceProvider();
                IProductService productService = builder.GetRequiredService<IProductService>();
                ILogger<Program> logger = builder.GetRequiredService<ILogger<Program>>();
                logger.LogInformation("哈哈");
                productService.Run();
                Console.ReadKey();
            }
            #endregion

            #region 1、控制台日志输出
            {
                // 1、创建日志 Hello, YDT_NET8_Logss!

                // 2、创建日志对象ILogger
                ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
                {
                    // 2.1、配置Console日志
                    builder.AddConsole();
                });
                // 2.2、创建ILogger接口
                ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

                // 2.3、输出日志
                logger.LogInformation("Hello, YDT_NET8_Logss!");
            }
            #endregion

            #region 2、文件日志输出
            {
                /* // 1、创建日志 Hello, YDT_NET8_Logss!

                 // 2、创建日志对象ILogger
                 ILoggerFactory loggerFactory = LoggerFactory.Create(builder => {
                     // 2.2、配置控制台日志
                     builder.AddConsole();
                     // 2.1、配置文件日志
                     builder.AddFile(options => {
                         // 1、配置文件根目录
                         options.RootPath = AppContext.BaseDirectory;
                         // 2、配置文件路径
                         options.Files = new[] {
                             new LogFileOptions{
                                 Path ="logs/log.txt"
                             }
                         };
                     });
                 });
                 // 2.2、创建ILogger接口
                 ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

                 // 2.3、输出日志
                 logger.LogInformation("Hello, YDT_NET8_Logss!");

                 Console.ReadKey();*/
            }
            #endregion

            #region 4、日志组件级别
            {
                /*// 1、创建日志 Hello, YDT_NET8_Logss!
                var connectionString = "server=localhost;database=ydt_net8_logss;user=root;password=root;";
                // 2、创建日志对象ILogger
                ILoggerFactory loggerFactory = LoggerFactory.Create(builder => {
                    // 2.2、配置控制台日志
                    builder.AddConsole().SetMinimumLevel(LogLevel.Information);
                    // 2.1、配置文件日志
                    builder.AddFile(options => {
                        // 1、配置文件根目录
                        options.RootPath = AppContext.BaseDirectory;
                        // 2、配置文件路径
                        options.Files = new[] {
                            new LogFileOptions{
                                Path ="logs/log.txt"
                            }
                        };
                    }).SetMinimumLevel(LogLevel.Information);

                    // 2.3、集成MySQL日志
                    builder.AddMySQL(connectionString).SetMinimumLevel(LogLevel.Information);
                });
                // 2.2、创建ILogger接口
                ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

                // 2.3、输出日志
                logger.LogTrace("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogDebug("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogInformation("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogWarning("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogError("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogCritical("Hello, YDT_NET8_Logss_LogLevel!");

                try
                {
                    throw new Exception("系统异常");
                }
                catch (Exception e)
                {
                    logger.LogError(e.Message);
                }
                Console.ReadKey();*/
            }
            #endregion

            #region 5、日志格式化输出
            {
                // 1、创建日志 Hello, YDT_NET8_Logss!
                var connectionString = "server=localhost;database=ydt_net8_logss;user=root;password=root;";
                // 2、创建日志对象ILogger
                ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
                {
                    // 2.2、配置控制台日志
                    builder.AddConsole().SetMinimumLevel(LogLevel.Information);
                    // 2.1、配置文件日志
                    builder.AddFile(options =>
                    {
                        // 1、配置文件根目录
                        options.RootPath = AppContext.BaseDirectory;
                        // 2、配置文件路径
                        options.Files = new[] {
                             new LogFileOptions{
                                 Path ="logs/log.txt"
                             }
                         };
                    }).SetMinimumLevel(LogLevel.Information);

                    // 2.3、集成MySQL日志
                    builder.AddSqlserver().SetMinimumLevel(LogLevel.Information);
                });
                // 2.2、创建ILogger接口
                ILogger<Program> logger = loggerFactory.CreateLogger<Program>();

                // 2.3、输出日志
                //*logger.LogTrace("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogDebug("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogInformation("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogWarning("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogError("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogCritical("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogInfoDefine("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogInformation("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogWarningDefine("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogErrorDefine("Hello, YDT_NET8_Logss_LogLevel!");
                logger.LogCriticalDefine("Hello, YDT_NET8_Logss_LogLevel!");
            }
            #endregion

            #region 6、日志组件应用
            {
                var connectionString = "server=localhost;database=ydt_net8_logss;user=root;password=root;";

                // 1、创建IOC
                ServiceCollection services = new ServiceCollection();
                services.AddLogging(options =>
                {
                    // 1、集成console日志
                    options.AddConsole().SetMinimumLevel(LogLevel.Debug);
                    // 2、集成文件日志
                    options.AddFile(options =>
                    {
                        // 1、配置文件根目录
                        options.RootPath = AppContext.BaseDirectory;
                        // 2、配置文件路径
                        options.Files = new[] {
                            new LogFileOptions{
                                Path ="logs/log.txt",
                                MaxFileSize =10 * 1024 *1024                            }
                        };
                    }).SetMinimumLevel(LogLevel.Information);
                    // 2、集成MySQL日志
                    options.AddSqlserver().SetMinimumLevel(LogLevel.Information);
                });

                // 2、注册OrderService
                services.AddSingleton<IProductService, ProductService>();

                // 3、取出OrderService
                ServiceProvider serviceProvider = services.BuildServiceProvider();
                ProductService orderService = serviceProvider.GetRequiredService<ProductService>();
                orderService.Run();

                Console.ReadKey();
            }
            #endregion

            #region 7、日志组件应用-日志级别化
            {
                var connectionString = "server=localhost;database=ydt_net8_logss;user=root;password=root;";

                // 1、创建配置组件
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.AddJsonFile("appsettings.json", true, true);
                IConfiguration configuration = builder.Build();

                // 2、创建IOC
                ServiceCollection services = new ServiceCollection();
                services.AddLogging(options =>
                {
                    // 读取配置文件中的日志配置
                    var logConfig = configuration.GetSection("Logss");
                    // 0、集成IConfiguration
                    options.AddConfiguration(logConfig);
                    // 1、集成console日志
                    options.AddConsole();
                    // 2、集成文件日志
                    options.AddFile(options =>
                    {
                        // 1、配置文件根目录
                        options.RootPath = AppContext.BaseDirectory;

                        // 读取配置文件中的文件日志配置
                        var fileConfig = logConfig.GetSection("File");
                        // 2、配置文件路径
                        options.Files = new[] {
                            new LogFileOptions{
                                Path =fileConfig["Path"],
                                MaxFileSize =fileConfig.GetValue<long>("MaxFileSize")
                            }
                        };
                    });
                    // 2、集成MySQL日志
                    options.AddSqlserver();
                });

                // 2、注册OrderService
                services.AddSingleton<IProductService, ProductService>();

                // 3、取出OrderService
                ServiceProvider serviceProvider = services.BuildServiceProvider();
                IProductService orderService = serviceProvider.GetRequiredService<IProductService>();
                orderService.Run();
                /*LogDebug(logger, "Hello, YDT_NET8_Logss_LogLevel!");
                LogInformation(logger, "Hello, YDT_NET8_Logss_LogLevel!");
                LogWarning(logger, "Hello, YDT_NET8_Logss_LogLevel!");
                LogError(logger, "Hello, YDT_NET8_Logss_LogLevel!");
                LogCritical(logger, "Hello, YDT_NET8_Logss_LogLevel!");*/

                Console.ReadKey();
            }
            #endregion

        }
    }
}
